package com.pwt.controller.watch;

import com.pwt.annotation.Update;
import com.pwt.common.CustomException;
import com.pwt.common.DataNotFoundException;
import com.pwt.entity.Hierarchy;
import com.pwt.entity.WhatLine;
import com.pwt.model.TreeModel;
import com.pwt.model.WhatLineModel;
import com.pwt.security.utils.JWTUser;
import com.pwt.security.utils.JWTUtils;
import com.pwt.service.AbilityLineService;
import com.pwt.service.WhatLineService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "生产线")
public class WhatLineController {

    @Autowired
    WhatLineService whatLineService;

    @Autowired
    AbilityLineService abilityLineService;

    @GetMapping("lines")
    @Operation(description = "列表",summary = "生产线列表")
    public List<WhatLine> list() {
        JWTUser user = JWTUtils.getToken();
        String username = user.getUsername();
        return whatLineService.list(username);
    }

    @GetMapping("lines/{id}")
    @Operation(description = "查询生产线",summary = "查询生产线")
    public WhatLineModel<String> view(@Parameter(description = "生产线ID") @PathVariable(name = "id") String id) throws DataNotFoundException {
        return whatLineService.view(id);
    }

    @PostMapping("lines")
    @Operation(description = "新增生产线",summary = "新增生产线")
    public WhatLine add(@RequestBody WhatLine model) throws CustomException {
        return whatLineService.add(model);
    }

    @PutMapping("lines")
    @Operation(description = "编辑生产线",summary = "编辑生产线")
    public WhatLine update(@RequestBody @Validated(Update.class) WhatLine model) throws CustomException {
        return whatLineService.add(model);
    }

    @DeleteMapping("lines/{id}")
    @Operation(description = "删除生产线",summary = "删除生产线")
    public void delete(@Parameter(description = "生产线ID") @PathVariable(name = "id") String id) throws CustomException {
        whatLineService.deleteWhatLine(id);
    }

    @GetMapping("lines/treeList")
    @Operation(description = "获取机器树形结构",summary = "获取机器树形结构")
    public List<Hierarchy> getHierarchyALL(){
        return this.whatLineService.queryAllHierarchy();
    }


    @GetMapping("lines/hierarchy-all")
    @Operation(description = "根据用户获取所有授权的工序和机器数据",summary = "根据用户获取所有授权的工序和机器数据")
    public List<TreeModel> getHierarchySelect(){
        return this.abilityLineService.getHierarchySelect();
    }


    @GetMapping("lines/whatLineId")
    @Operation(description = "根据产线id查询",summary = "根据产线id查询")
    public List<Hierarchy> getHierarchyByWhatLineId(String whatLineId){
        return this.whatLineService.queryHierarchyByWhatLineId(whatLineId);
    }
}
